<?php

namespace App\Http\Controllers\Api\Admin;

use App\Classify;
use App\Http\Services\FileManager;
use App\ShopAdmin;
use App\Shops;
use App\ShopsAdmin;
use App\User;
use App\UserInfo;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ShopController extends Controller
{
    /**
     * 设置店铺
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setShop(Request $request){
        $id         = !is_null($request->get('id'))      ? $request->get('id'):0;
        $thumb_id   = !is_null($request->get('thumb_id'))? $request->get('thumb_id') : 0;
        $name       = !is_null($request->get('name'))    ? $request->get('name') : '';
        $sort       =!is_null($request->get('sort'))    ? $request->get('sort') : 0;
        $province   = $request->get('province');
        $city       = $request->get('city');
        $area       = $request->get('area');
        $address    = $request->get('address');
        $shipping_way=$request->get('shipping_way');

        if (!$name)
            return response()->json(['level'=>'error','message'=>'请填写店铺名称']);
        if (!$province)
            return response()->json(['level'=>'error','message'=>'请选择省']);
        if (!$city)
            return response()->json(['level'=>'error','message'=>'请选择城市']);
        if (!$area)
            return response()->json(['level'=>'error','message'=>'请选择地区']);
        if (!$address)
            return response()->json(['level'=>'error','message'=>'请填写地址']);
        $shop = Shops::whereId($id)->first();
        if (!$shop)
            $shop=new Shops();

        if ($thumb_id)  $shop->thumb_id = $thumb_id;
        if ($name)      $shop->name     = $name;
        if ($sort)      $shop->sort     = $sort;
        if ($sort)      $shop->sort     = $sort;
        if ($province)  $shop->province = $province;
        if ($city)      $shop->city     = $city;
        if ($area)      $shop->area     = $area;
        if ($address)   $shop->address  = $address;
        $shop->shipping_way             = 'self';
        $shop->save();

        return response()->json(['level'=>'success','message'=>'操作成功','data'=>$shop]);
    }

    /**
     * 获取店铺列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getShopList(Request $request){
        $keyword= !is_null($request->get('keyword'))     ? $request->get('keyword')  : '';
        $page   = !is_null($request->get('page'))   ? $request->get('page')     : 1;
        $limit  = !is_null($request->get('limit'))  ? $request->get('limit')    : 10;
        $pageInfo=[
            'limit'=>$limit,
            'page'=>$page,
            'total'=>0
        ];

        $page=$page-1;
        /** @var Shops $item */
        $shop=Shops::whereNull('deleted_at');

        if ($keyword){
            $shop=$shop->where('name','like','%'.trim($keyword).'%');
        }
        $pageInfo['total']=$shop->count();

        $shop=$shop->orderBy('created_at','desc')->offset($page * $limit)->limit($limit)->get();
        if (count($shop) > 0){
            foreach ($shop as $item){
                $item->thumb_url = FileManager::getImgUrl($item->thumb_id);
            }
        }

        return response()->json(['level'=>'success','message'=>'获取成功','data'=>$shop,'page_info' => $pageInfo]);
    }

    /**
     * 删除店铺
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function delShop(Request $request){
        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $shop=Shops::whereId($id)->first();
        if (!$shop)
            return response()->json(['level'=>'error','message'=>'该店铺不存在']);

        $shop->delete();

        return response()->json(['level'=>'success','message'=>'删除成功']);
    }

    /**
     * 获取店铺管理员
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getShopAdmin(Request $request){
        $shop_id = !is_null($request->get('shop_id')) ? $request->get('shop_id') : 0;

        /** @var ShopAdmin $item */
        $shopAdmin = ShopAdmin::whereShopId($shop_id)->orderBy('created_at','desc')->get();
        if (count($shopAdmin) > 0){
            foreach($shopAdmin as $item){
                $users=User::whereId($item->user_id)->first();
                $userInfo = UserInfo::whereUserId($item->user_id)->first();
                $item->realname = $userInfo ? $userInfo->realname ? $userInfo->realname : $userInfo->nickname ? $userInfo->nickname : '' : '';
                $item->phone    = $users ? $users->phone : '';
            }
        }

        return response()->json(['level'=>'success','message'=>'获取成功','data' => $shopAdmin]);
    }

    /**
     * 添加店铺管理员
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addShopAdmin(Request $request){
        $shopId = !is_null($request->get('shop_id')) ? $request->get('shop_id') : 0;
        $userId = !is_null($request->get('shop_id')) ? $request->get('shop_id') : 0;

        $user = User::whereId($userId)->first();
        if(!$user)
            return response()->json(['level' => 'error','message0' => '用户不存在']);

        $newShopAdmin = ShopAdmin::whereUserId($user->id)->whereShopId($shopId)->first();
        if($newShopAdmin)
            return response()->json(['level' => 'error','message' => '该用户已经是']);

        $newShopAdmin = new ShopAdmin();
        $newShopAdmin->user_id      = $user->id;
        $newShopAdmin->shop_id      = $shopId;
        $newShopAdmin->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $newShopAdmin]);
    }

    /**
     * 删除店铺管理员
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function delShopAdmin(Request $request){
        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $shopAdmin = ShopAdmin::whereId($id)->first();
        if(!$shopAdmin)
            return response()->json(['level' => 'error','message' => '店铺管理员不存在']);

        $shopAdmin->delete();

        return response()->json(['level' => 'success','message' => '删除成功']);
    }

}
